<template>
  <div class='login-container'>
    <el-card class='login-form'>
      <el-form ref='registerFormRef' :model='registerForm'>
        <el-form-item label='' prop='username' label-width='0'>
          <el-input v-model='registerForm.username' class='input-style' placeholder='请输入用户名'
                    autocomplete='off new-password'></el-input>
        </el-form-item>
        <el-form-item label='' prop='password' label-width='0'>
          <el-input type='password' v-model='registerForm.password' show-password class='input-style'
                    placeholder='请输入密码'></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type='primary' @click='submitForm' class='login-button'>注册</el-button>
        </el-form-item>
        <div class="center-flex">
          <el-link type="primary" style='font-family: "SimHei", sans-serif;' @click='toLogin'>返回登录</el-link>
        </div>
      </el-form>
    </el-card>

  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import {register} from '/src/api/index'

const router = useRouter()

const registerForm = ref({
  username: '',
  password: ''
})

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 1, max: 18, message: '密码长度应为6到18个字符', trigger: 'blur' }
  ]
}

const submitForm = () => {

  // 这里可以添加你的注册逻辑，比如发送请求到后端
  register(registerForm.value).then(resp => {
    ElMessage.success('注册成功！')
    // 清空表单
    resetForm()
    // 跳转到登录页面
    toLogin()
  }).catch(e => {
    ElMessage.error('注册失败')
  })
}

const resetForm = () => {
  registerForm.value = {}
}

const toLogin = () => {
  router.push('/login')
}
onMounted(() => {

})
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
  background-color: #000000;
}

.login-form {
  width: 20%;
  background-color: #ABABAB; /* 表单白色 */
  color: #333;
}

.input-style {
  background-color: #fff; /* 输入框背景白色 */
  color: #000; /* 输入框文本黑色 */
  border-color: #fff; /* 输入框边框白色 */
}

.input-style::placeholder {
  color: #000; /* 输入框占位符文本黑色 */
}

.login-button {
  width: 100%; /* 按钮宽度与表单宽度一致 */
  background-color: #217ede;
  border-color: #217ede;
  color: white;
}

.login-button:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

/*解决浏览器输入框在记住密码后改变颜色问题*/
input:-webkit-autofill {
  /* 自动填充文字颜色 */
  -webkit-text-fill-color: #24515d !important;
  transition: background-color 5000s ease-in-out 0s;
}
.center-flex {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center;     /* 垂直居中 */
  height: 100%;            /* 确保容器占据整个高度 */
}
.el-input >>> .el-input__inner {
  color: black; /* 你想要的颜色 */
}
</style>